// localLifePackage/pages/writeOff/writeOff.js
var app = getApp();

var common = require("../../../common/common.js").common;

Page({
    /**
   * 页面的初始数据
   */
    data: {
        title: "取货核销",
        height: 0,
        inputValue: "",
        order_sn: 0,
        //订单号
        code: 0,
        //核销码
        num: 6
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        var w = wx.getSystemInfoSync().windowWidth;
        var h = wx.getSystemInfoSync().windowHeight;
        this.setData({
            height: 759 / w * h
        });
        this.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        //首次直接进入，拿user_id
                if (app.globalData.user) {
            //如果直接拿到openid
            this.setData({
                uid: app.globalData.user.uid
            });
        } else {
            //回调拿到openid
            app.userInfoReadyCallback = (res => {
                this.setData({
                    uid: res.uid
                });
            });
        }
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    passwordNum: function(e) {
        this.setData({
            inputValue: e.detail
        });
    },
    toConfirmWriteOrder: function() {
        let that = this;
        if (that.data.inputValue.length < 6) {
            wx.showModal({
                title: "提示",
                content: "请输入完整的验证码",
                showCancel: false
            });
            return;
        }
        common.request({
            endpoint: "Localbusiness/order/order-detail-by-uid",
            method: "post",
            data: {
                user_id: that.data.uid,
                code: that.data.inputValue
            },
            success: res => {
                console.log(res);
                if (res.extra.order_sn) {
                    wx.navigateTo({
                        url: "/localLifePackage/pages/writeOrder/writeOrder?code=" + this.data.inputValue + "&info=" + encodeURIComponent(JSON.stringify(res.extra))
                    });
                } else {
                    wx.showModal({
                        title: "提示",
                        content: "核销码不正确，请重新输入",
                        showCancel: false,
                        success(res) {}
                    });
                    return;
                }
            }
        });
    }
});